<template>
	<view class="container">
		<!-- 顶部导航 -->
		<view class="header">
			<view class="cancel-btn" @click="toBack">取消</view>
			<view class="publish-btn" @click="submit">
				发布
			</view>
		</view>
		
		<!-- 内容区域 -->
		<view class="content-wrapper">
			<view class="form-card">
				<view class="form-item title-item">
					<text class="form-label">请输入标题</text>
					<input placeholder="请输入标题..." class="form-input" @input="titleInput($event)" />
				</view>
				<view class="form-item content-item">
					<text class="form-label">输入想法</text>
					<textarea placeholder="请输入内容..." class="form-textarea" maxlength="2000"
						@input="descInput($event)" data-model="desc" />
					<view class="word-count">
						{{remnant}}/2000
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 保持原有逻辑代码不变
	export default {
		data() {
			return {
				remnant: "0",
				postAllId: 0,
				addForm: {
					postAllId: 0,
					postUserId: 0,
					postContent: null,
					postTitle: null
				},
				tokenList: []
			};
		},
		onLoad(option) {
			this.addForm.postAllId = option.postAllId
			this.tokenList = uni.getStorageSync("storage_data") || {};
			this.addForm.postUserId = this.tokenList.vuex_id
		},
		methods: {
			descInput: function(e) {
				this.remnant = e.target.value.length;
				this.addForm.postContent = e.target.value;
			},
			titleInput: function(e) {
				this.addForm.postTitle = e.target.value
			},
			toBack() {
				uni.navigateBack({
					delta: 1,
				})
			},
			submit() {
			  uni.request({
			    url: this.$BASE_URL.baseUrl + '/community/post',
			    method: 'post',
			    header: {
			      'Content-Type': 'application/json'
			    },
			    data: {
			      postAllId: this.addForm.postAllId,
			      postUserId: this.addForm.postUserId,
			      postContent: this.addForm.postContent,
			      postTitle: this.addForm.postTitle
			    },
			    success: () => {
			      // 发布成功后，通过事件总线通知父页面刷新
			      uni.$emit('postPublished');
			      
			      // 清空表单
			      this.addForm = {
			        postAllId: this.postAllId,
			        postUserId: this.tokenList.vuex_id,
			        postContent: null,
			        postTitle: null
			      };
			      this.remnant = "0";
			      
			      // 返回并刷新
			      uni.navigateBack({
			        delta: 1,
			        success: () => {
			          uni.showToast({
			            title: '发布成功',
			            icon: 'success'
			          });
			        }
			      });
			    },
			    fail: () => {
			      uni.showToast({
			        title: '发布失败',
			        icon: 'error'
			      });
			    }
			  });
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		background-color: #f8f4e9;
		min-height: 100vh;
		padding: 20rpx;
	}
	
	/* 顶部导航样式 */
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		margin-bottom: 20rpx;
		
		.cancel-btn {
			color: #7C191E;
			font-size: 32rpx;
			font-weight: 500;
		}
		
		.publish-btn {
			background-color: #7C191E;
			color: white;
			padding: 12rpx 36rpx;
			border-radius: 40rpx;
			font-size: 32rpx;
			font-weight: 500;
		}
	}
	
	/* 内容区域 */
	.content-wrapper {
		padding: 20rpx;
	}
	
	.form-card {
		background-color: #fff;
		border-radius: 24rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
	}
	
	.form-item {
		margin-bottom: 40rpx;
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #f0e6d6;
		
		&:last-child {
			border-bottom: none;
			margin-bottom: 0;
			padding-bottom: 0;
		}
	}
	
	.form-label {
		display: block;
		font-size: 32rpx;
		color: #5e3a1e;
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	
	.form-input {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		font-size: 30rpx;
		color: #3a2818;
		background-color: #f9f5eb;
		border-radius: 12rpx;
	}
	
	.form-textarea {
		width: 100%;
		height: 400rpx;
		padding: 20rpx;
		font-size: 30rpx;
		color: #3a2818;
		background-color: #f9f5eb;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
	}
	
	.word-count {
		text-align: right;
		font-size: 24rpx;
		color: #a68a64;
	}
</style>